###############################################################################
## AUTHOR: ALAN YAN
## DATE LAST UPDATED: 07/26/2020
## PURPOSE: DESCRIPTIVE STATS
###############################################################################
rm(list = ls())

#### todo ####


#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       gridExtra,
       egg,
       car)

#### FUNCTIONS ####

# ggplot theme
theme_shom_alt <- function (base_size = 11, waffle = FALSE) 
{
  ret <- theme_minimal(base_size = base_size, base_family = "Roboto Condensed") + 
    theme(plot.background = element_rect(fill = "#f5f5f2", 
                                         color = NA), 
          panel.background = element_rect(fill = "#f5f5f2",color = NA), 
          legend.background = element_rect(fill = "#f5f5f2",color = NA))
  if (waffle) {
    ret + theme(axis.text = element_blank())
  }
  else {
    ret
  }
}

# lm robust function that includes omitted categories for plotting
lm_robust_omitted <- function(formula, data, clusters,weights = NULL) {
  require(formula.tools)
  require(tidyverse)
  require(haven)
  results <- lm_robust(formula = formula,
                       data = data,
                       clusters = clusters,
                       weights = weights) %>%
    tidy() 
  omitted_cat <- model.frame(formula = formula, data = data) %>% 
    select(2:length(.)) %>% 
    sapply(., levels) %>% 
    lapply(., "[[", 1) %>% 
    unlist() %>%
    as.data.frame()
  omitted_cat_df <- cbind(paste0(rownames(omitted_cat), omitted_cat[,1]), 
                          0, 0, 0, 0, 0, 0, 0, 
                          lhs.vars(formula)) %>%
    as.data.frame() %>%
    zap_labels() %>%
    setNames(., names(results))
  results_with_omitted <- rbind(results, 
                                omitted_cat_df)
  results_with_omitted[,c(2:8)] <- sapply(results_with_omitted[,c(2:8)],as.numeric)
  return(results_with_omitted)
}

#### LOAD DATA ####
df <- read_rds("01-yougov-conjoint/01-data/cleaned/yougov-conjoint-cleaned.rds")

#### MAKE TABLE ####
p_load(stargazer)

#### *DUMMYING CATEGORICAL VARIBLES ####
#Sex
for(i in unique(df$r_female)) {
  df[i] <- ifelse(df$r_female == i, 1, 0)
}

#Education
for(i in unique(df$r_edu)) {
  df[i] <- ifelse(df$r_edu == i, 1, 0)
}

#Race
for(i in unique(df$r_race)) {
  df[i] <- ifelse(df$r_race == i, 1, 0)
}

#Hispanic
for(i in unique(df$r_hispanic)) {
  df[i] <- ifelse(df$r_hispanic == i, 1, 0)
}

#Employment
for(i in unique(df$r_employment)) {
  df[i] <- ifelse(df$r_employment == i, 1, 0)
}

#Union
for(i in unique(df$r_union_member)) {
  df[i] <- ifelse(df$r_union_member == i, 1, 0)
}

covariates <- names(df)[38:62]

#GENERATES TABLE S9
df[match(unique(df$rid), df$rid),] %>%
  select(covariates) %>%
  as.data.frame() %>%
  stargazer(., summary = TRUE,
            summary.stat = c("mean", "sd", "n"))
